<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            margin: auto;
            width: 720px;
        }
        /* 通过column-count实现 */
        /* .masonry {
            column-count: 4;
            column-gap: 0
        }
        .masonry .item {
            padding: 2px;
            margin: 2px;
            position: relative;
            counter-increment: item-counter;
        }
        .masonry .item img {
            display: block;
            width: 100%;
            height: auto;
        }
        .item::after {
            position: absolute;
            display: block;
            top: 2px;
            left: 2px;
            width: 24px; 
            height: 24px;
            background-color: #fff;
            color: brown;
            content: counter(item-counter);
        } */
        /* 通过Flexbox实现 */
        .masonry {
            display: flex;
            flex-direction: column;
            flex-wrap: wrap;
            height: 1200px;
        }
        .item {
            width: 25%;
            padding: 2px;
            margin: 2px;
            position: relative;
            counter-increment: item-counter;
        }
        .item img{
            display: block;
            height: auto;
            width: 100%;
        }
        .item::after {
            display: block;
            position: absolute;
            top: 2px;
            left: 2px;
            width: 24px;
            height: 24px;
            background-color: #fff;
            text-align: center;
            content: counter(item-counter);
        }
        .item:nth-child( 4n + 1 ) {
            order: 1;
        }
        .item:nth-child( 4n + 2 ) {
            order: 2;
        }
        .item:nth-child( 4n + 3 ) {
            order: 3;
        }
        .item:nth-child( 4n ) {
            order: 4;
        }
    </style>
    <title>Masonry</title>
</head>
<body>
    <div class="masonry">
        <div class='item'><img src="https://picsum.photos/360/460?random=1" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/480?random=2" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/440?random=3" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/420?random=4" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/480?random=5" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/480?random=6" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/420?random=7" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/400?random=8" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/460?random=9" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/440?random=10" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/420?random=11" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/400?random=12" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/480?random=13" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/460?random=14" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/480?random=15" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/440?random=16" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/420?random=17" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/400?random=18" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/440?random=19" alt=""></div>
        <div class='item'><img src="https://picsum.photos/360/460?random=20" alt=""></div>
    </div>
</body>
</html>